tool
extends EditorScript



var text = """
Field	Type	Collation	Null	Key	Default	Extra	Privileges	Comment
id

varchar(50)

utf8_general_ci

YES

 

(NULL)

 

select,insert,update,references

领导id

name

varchar(20)

utf8_general_ci

YES

 

(NULL)

 

select,insert,update,references

姓名

prison_id

varchar(20)

utf8_general_ci

YES

 

(NULL)

 

select,insert,update,references

监所id

duty

varchar(20)

utf8_general_ci

YES

 

(NULL)

 

select,insert,update,references

领导职务

contact_information

varchar(50)

utf8_general_ci

YES

 

(NULL)

 

select,insert,update,references

联系电话

zhzx_number

varchar(20)

utf8_general_ci

YES

 

(NULL)

 

select,insert,update,references

指挥中心联系方式

kssmc

varchar(20)

utf8_general_ci

YES

 

(NULL)

 

select,insert,update,references

看守所名称
"""

func _run():
	
	# 头文字
	var head_text_regex = RegEx.new()
	var head_text_pattern = "([^\n]+)\n"
	head_text_regex.compile(head_text_pattern)
	var head_result : RegExMatch = head_text_regex.search(text)
	var head_text : String = head_result.get_string()
	
	# 头标题
	var heads = []
	for result in head_text.split("\t"):
		heads.append(result.strip_edges())
	
	# 表格体
	text = text.replace(head_text, "")
	var table_pattern = "\\n([^\\n]+)\\n"
	var table_regex = RegEx.new()
	table_regex.compile(table_pattern)
	var results = table_regex.search_all(text)
	var item_list = []
	for result in results:
		item_list.append(result.get_string(1))
	
	# 表格体数据
	var column_count = heads.size()
	var count = item_list.size()
	var tables = []
	for i in count:
		var column = i % column_count
		var key = heads[column]
		if column == 0:
			tables.append({})
		tables.back()[key] = item_list[i]
	
	var html = """<mxGraphModel><root>
	<mxCell id="0"/>
	<mxCell id="1" parent="0"/>"""
	var origin = Vector2(1000, 1000)
	var pos = origin
	var size = Vector2(80, 40)
	for item in tables:
		pos = Vector2(origin.x, pos.y + 60)
		var field = item["Field"]
		html += draw_io_shape(Rect2(pos, size), field)
	html += "\n</root></mxGraphModel>"
	OS.clipboard = html
	print("* 生成的代码已复制到剪贴板中")



var id = 2
func get_id() -> int:
	id += 1
	return id


func draw_io_shape(
	rect: Rect2, 
	label:String, 
	type: String = "rounded=0"
):
	var tag = """
	<mxCell id="{id}" value="{value}" style="{type};whiteSpace=wrap;html=1;" vertex="1" parent="1">
		<mxGeometry x="{x}" y="{y}" width="{width}" height="{height}" as="geometry"/>
	</mxCell>""".format({
		"id": get_id(),
		"x": rect.position.x,
		"y": rect.position.y,
		"width": rect.size.x,
		"height": rect.size.y,
		"value": label,
		"type": type,
	})
	return tag


func draw_io_line(source_id: int, target_id: int):
	var code = """
	<mxCell id="{id}" style="rounded=0;orthogonalLoop=1;jettySize=auto;html=1;endArrow=none;endFill=0;"
			edge="1" source="{source}" target="{target}" parent="1">
		<mxGeometry relative="1" as="geometry" />
	</mxCell>""".format({
		"id": get_id(),
		"source": source_id,
		"target": target_id,
	})



